package com.system.service.impl;

import com.system.mapper.RoleMapper;
import com.system.po.Role;
import com.system.po.permission;
import com.system.po.role_permission;
import com.system.service.AssignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Transactional
@Service
public class AssignServiceimpl implements AssignService {

     @Autowired
     private RoleMapper roleMapper;

     @Override
     public List<Role> findRoles() {
     List<Role> roles=roleMapper.findRoles();
     return roles;
    }


    @Override
    public List<permission> Selectpermissions() {
        List<permission> Selectpermissions=roleMapper.Selectpermissions();
        return Selectpermissions;
    }

    @Override
    public int adddistriBution(Integer id, List<permission> list) {

        try {
            List<role_permission> list2 = new ArrayList<>();


            for (int i = 0; i < list.size(); i++) {
                role_permission role = new role_permission();
                role.setRole_id(id);
                role.setPermission_id(list.get(i).getId());
                list2.add(role);
            }

            return roleMapper.adddistriBution(list2);

        } catch (Exception e) {
            e.printStackTrace();
            return  0;
        }

    }
}
